package game 
{
	import flash.display.Sprite;
	import game.commands.StartGameCommand;
	import game.gui.Gui;
	import game.map.Hero;
	import game.map.Map;
	import game.map.Province;
	import game.map.ProvinceTypes;
	import game.map.Unit;
	import game.map.UnitTypes;
	import game.mapviewer.MapViewer;
	import game.phases.ActionPhase;
	import game.phases.IncomePhase;
	import game.phases.Phase;
	import game.phases.PlanningPhase;
	import game.phases.RecruitPhase;
	import game.unitmanager.UnitsManager;
	/**
	 * ...
	 * @author rnk
	 */
	public class Game extends Sprite
	{
		public var mapModel:Map; //model
		public var mapView:MapViewer; //view
		public var unitsManager:UnitsManager; //units controllers
		public var players:Array; //players
		public var turn:int = 1; // current turn
		public var gui:Gui; // gui
		
		private var phases:Array;
		private var currentPhase:Phase;
		private var currentPhaseIdx:int = -1;
		
		
		public function Game() 
		{
		}
		
		public function Init():void
		{
			//phases
			phases = [
				//IncomePhase,
				//RecruitPhase,
				PlanningPhase,
				ActionPhase
			];
			
			//map model
			mapModel = new Map();
			mapModel.Init( Settings.MAP1 );
			
			//map view
			mapView = new MapViewer();
			addChild(mapView);
			mapView.papa = this;
			mapView.Init(mapModel);
			//mapView.DrawRoutes();
			
			//units manager
			unitsManager = new UnitsManager();
			unitsManager.papa = this;
			
			//gui
			gui = new Gui();
			addChild(gui);
			gui.papa = this;
			gui.Init();
			
			//start game
			(new StartGameCommand(this)).Execute();
			
		}
		
		public function SetPhase(phaseClass:Class):void
		{
			if (currentPhase)
			{
				currentPhase.Exit();
				currentPhase.papa = null;
				currentPhase = null;
			}
			
			currentPhase = new phaseClass();
			currentPhase.papa = this;
			currentPhase.Enter();
		}
		
		public function NextPhase():void
		{
			currentPhaseIdx++;
			if (currentPhaseIdx >= phases.length)
			{
				turn++;
				currentPhaseIdx = 0;
			}
			SetPhase(phases[currentPhaseIdx]);
		}
		
		public function GetPlayer(uid:int):Player
		{
			for each (var item:Player in players) 
			{
				if (item.uid == uid ) return item;
			}
			return null;
		}
	}

}